﻿function updateDataScheduleViewModel(parent) {
    var self = this;

    if (parent != null)
        $.extend(this, parent);

    self.query = ko.observable("Twitter API");
    self.isLoading = ko.observable(true);
    self.result = ko.observable({
        Region: {
            Name: "",
            Weather: []
        }, 
        Tweets: [] 
    });
    self.error = ko.observable({ show: false });  

        
    self.search = function () {
        self.loadData({
            address: "data/get/" + self.query(),
            onsuccess: function (data) {
                self.result(data);
            }
        });
    };

    self.loadData = function (arg) {
        ko.dependentObservable(function () {

            self.isLoading(true);

            $.ajax({
                type: 'GET',
                url: '/api/v1.0/' + arg.address,
                data: arg.params,
                success: function (data) {
                    if (arg.onbefore !== undefined)
                        arg.onbefore();

                    if (arg.obj !== undefined) {
                        arg.obj([]);
                        $.each(data, function (index, item) {
                            arg.obj.push(item);
                        });
                    }

                    if (arg.onsuccess !== undefined)
                        arg.onsuccess(data);

                    self.isLoading(false);
                },
                error: function (x, h, r) {
                    if (arg.onerror !== undefined)
                        arg.onerror(x, h, r);
                    else
                        self.error({ show: true, message: "Internal server error" });

                    self.isLoading(false);
                }
            });

            if (arg.after !== undefined)
                arg.after();

        }, this);
    }

    self.sendData = function (arg) {
        self.isLoading(true);

        if (arg.onbefore !== undefined)
            arg.onbefore(obj);

        $.ajax({
            type: 'POST',
            url: '/api/v1.0/' + arg.address,
            data: arg.params,
            success: function (data) {

                if (arg.onsuccess !== undefined)
                    arg.onsuccess(data);

                self.isLoading(false);
            },
            error: function () {
                if (arg.onerror !== undefined)
                    arg.onerror();

                self.isLoading(false);
            }
        });
    };

    // Initialize object
    this.init = function () {

        self.loadData({
            address: "data/get/" + self.query(),
            onsuccess: function (data) {
                self.result(data);
            }
        });

        $(".content").jScrollPane({ autoReinitialise: true });
        $(".left-menu").jScrollPane({
            autoReinitialise: true
        });
        $("a, button, input").tooltip({ placement: 'bottom' });
        $("select").tooltip({ placement: 'bottom' });

        $(".modal").modal({
            backdrop: false,
            keyboard: false,
            show: false
        });        
    };
};

$(function () {
    var schedule = new updateDataScheduleViewModel();

    schedule.init();

    ko.applyBindings(schedule);
})